package com.ghd.dataviz.core.constant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
@AllArgsConstructor
public enum ResponseCode {

    SUCCESS(20000, "请求成功"),
    BAD_CREDENTIALS(40101, "密码错误"),
    UN_KNOW_OR_UNUSED_USER(40102, "未知用户或停用用户"),
    EXPIRED_JWT(40103, "令牌过期"),
    FILE_NOT_FOUND(50001, "未选择上传文件"),
    UN_KNOW_EXCEPTION(50099, "未知异常");

    private static Map<Integer, ResponseCode> map;

    static {
        map = Stream.of(ResponseCode.values())
                .collect(Collectors.toMap(ResponseCode::getCode, Function.identity(),
                        (k, v) -> k.ordinal() > v.ordinal() ? k : v ));
    }

    @JsonValue
    private Integer code;

    private String desc;

    @JsonCreator
    public static ResponseCode of(Integer code) {
        return map.get(code);
    }
}
